| Conditions | 1 |
| Paths | 1 |
| Total Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 16 | function elements(state, data) { |
||
| 17 | let ct = this; |
||
| 18 | ct.state = state; |
||
| 19 | ct.data = data; |
||
| 20 | |||
| 21 | ct.elementPrice = function (element) { |
||
| 22 | return Math.floor(Math.pow(data.constants.ELEMENT_PRICE_INCREASE, state.player.elements_unlocked) * |
||
| 23 | data.elements[element].number); |
||
| 24 | }; |
||
| 25 | |||
| 26 | function isElementCostMet(element) { |
||
| 27 | let price = ct.elementPrice(element); |
||
| 28 | return state.player.resources.dark_matter.number >= price; |
||
| 29 | } |
||
| 30 | |||
| 31 | ct.buyElement = function (element) { |
||
| 32 | if (state.player.elements[element].unlocked) { |
||
| 33 | return; |
||
| 34 | } |
||
| 35 | if (isElementCostMet(element)) { |
||
| 36 | let price = ct.elementPrice(element); |
||
| 37 | state.player.resources.dark_matter.number -= price; |
||
| 38 | |||
| 39 | state.player.elements[element].unlocked = true; |
||
| 40 | state.player.elements[element].generators['1'] = 1; |
||
| 41 | state.player.elements_unlocked++; |
||
| 42 | } |
||
| 43 | }; |
||
| 44 | |||
| 45 | /* This function returns the class that determines on which |
||
| 46 | colour an element card */ |
||
| 47 | ct.elementClass = function (element) { |
||
| 48 | if(!state.player.elements[element]){ |
||
| 49 | return 'element_unavailable'; |
||
| 50 | } |
||
| 51 | if (state.player.elements[element].unlocked) { |
||
| 52 | return 'element_purchased'; |
||
| 53 | }else{ |
||
| 54 | if(isElementCostMet(element)) { |
||
| 55 | return 'element_cost_met'; |
||
| 56 | }else{ |
||
| 57 | return 'element_cost_not_met'; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | }; |
||
| 61 | |||
| 62 | /* This function returns the class that determines the secondary |
||
| 63 | colour of an element card */ |
||
| 64 | ct.elementSecondaryClass = function (element) { |
||
| 65 | return ct.elementClass(element) + '_dark'; |
||
| 66 | }; |
||
| 67 | } |
||
| 68 |